/**
 *
 */
package com.coo.tao.web.controller.base;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

import com.coo.tao.core.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lizhipeng
 */
public class BaseController {
    public static int PAGESIZE = 20;
    public static final String ERROR_URL = "/error/error";//错误视图url
    public static final String MSG = "msg";//通用标识key
    public static final String MARK = "mark";//通用标识key
    public static final String YES = "yes";//通用标识value
    public static final String NO = "no";//通用标识value
    public static final String ERROR = "ERROR";//通用标识value

    public String getLastHttpPath(HttpServletRequest request) {
        String lastHttpPath = "";
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if ("lastHttpPath".equals(name)) {
                    try {
                        lastHttpPath = URLDecoder.decode(cookie.getValue(), "utf-8");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
        if (StringUtils.isNotEmpty(lastHttpPath))
            return lastHttpPath;
        else
            return null;

    }


    public void removeLastHttpPath(HttpServletRequest request, HttpServletResponse response) {
        this.removeCookiItem("lastHttpPath", request, response);
    }

    /**
     * 只返回错误标识不返回视图
     *
     * @param model
     * @return
     */
    public Model error(Model model) {
        model.addAttribute(MARK, ERROR);
        return model;
    }

    /**
     * 跳转到错误页面
     *
     * @return
     */
    public ModelAndView error(ModelAndView mv) {
        mv.addObject(MARK, ERROR);
        mv.setViewName(ERROR_URL);
        return mv;
    }

    /**
     * 跳转到错误页面并输出错误信息
     *
     * @param msg
     * @return
     */
    public ModelAndView error(ModelAndView mv, String msg) {
        mv.addObject(MSG, msg);
        mv.setViewName(ERROR_URL);
        return mv;
    }

    @InitBinder
    public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws ServletException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /**
     * 全站范围内添加缓存
     *
     * @param key
     * @param value
     * @param time
     * @param request
     * @param response
     */
    public static void addCookiItem(String key, String value, int time, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (value != null) {
                value = URLEncoder.encode(value, "utf-8");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Cookie cookie = new Cookie(key, value);
        cookie.setMaxAge(time);
        String path = request.getContextPath();
        if (StringUtils.isEmpty(request.getContextPath())) {
            path = "/";
        }
        cookie.setPath(path);
        response.addCookie(cookie);
    }

    public void removeCookiItem(String key, HttpServletRequest request, HttpServletResponse response) {
        Cookie cookie = new Cookie(key, null);
        cookie.setMaxAge(0);
        String path = request.getContextPath();
        if (StringUtils.isEmpty(request.getContextPath())) {
            path = "/";
        }
        cookie.setPath(path);
        response.addCookie(cookie);
    }


    /**
     * 从request里面获得整型参数
     *
     * @param paramMap
     * @param key
     * @return
     */
    public Integer getParamFromRequest(Map<String, Object> paramMap, String key) {
        String[] param = (String[]) paramMap.get(key);
        if (param != null && param.length > 0) {
            return Integer.valueOf(param[0]);
        } else {
            return null;
        }
    }

    /*public static void addVenderCookiItem( String value, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (value != null) {
                value = URLEncoder.encode(value, "utf-8");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        StringBuilder sb = new StringBuilder(value);
        sb.append("|");
        sb.append(System.currentTimeMillis());
        String cookieStr = StringUtils.encrypt(sb.toString());


        Cookie cookie = new Cookie(CommonInterceptor.COOKI_KEY_SHOP, cookieStr);
        String path = request.getContextPath();
        if (StringUtils.isEmpty(request.getContextPath())) {
            path = "/";
        }
        cookie.setPath(path);
        response.addCookie(cookie);
    }*/


	
}
